﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UwinEducation.Application.Common.Helpers;
using UwinEducation.Application.Dtos;
using UwinEducation.Application.Extensions;
using UwinEducation.Domain.Entities;
using UwinEducation.Shared.Enums;


namespace UwinEducation.Application.Mappings
{
    public class StudentScheduleProfile : Profile
    {
        public StudentScheduleProfile()
        {
            // int _year = ServiceLocator.GetConfigIntValue("grade11Month", 10); // 默认10月
            // int _month = ServiceLocator.GetConfigIntValue("grade11Data", 1);  // 默认1日

            CreateMap<StudentSchedule, StudentScheduleDto>()
                .ForMember(dest => dest.StudentNo, opt => opt.MapFrom(src => src.Student.StudentNo))
                .ForMember(dest => dest.StudentName, opt => opt.MapFrom(src => src.Student.Name))
                .ForMember(dest => dest.School, opt => opt.MapFrom(src => src.Student.School))
                .ForMember(dest => dest.TeacherScheduleGroupId, opt => opt.MapFrom(src => src.TeacherScheduleId))
                .ForMember(dest => dest.TeacherScheduleId, opt => opt.MapFrom(src => src.TeacherScheduleDetailsId))
                .ForMember(dest => dest.StartTime, opt => opt.MapFrom(src => src.TeacherScheduleDetails.StartTime))
                .ForMember(dest => dest.EndTime, opt => opt.MapFrom(src => src.TeacherScheduleDetails.EndTime))
                .ForMember(dest => dest.Grade, opt => opt.MapFrom<StudentScheduleGradeResolver>()) // 使用专用解析器
                .ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.Status))
                .ForMember(dest => dest.Reason, opt => opt.MapFrom(src => src.Reason))
                .ForMember(dest => dest.Remarks, opt => opt.MapFrom(src => src.Remarks));

            CreateMap<CreateStudentScheduleDto, StudentSchedule>()
                 .ForMember(dest => dest.TeacherScheduleDetailsId, opt => opt.MapFrom(src => src.TeacherScheduleId))
                 .ForMember(dest => dest.TeacherScheduleId, opt => opt.MapFrom(src => src.TeacherScheduleGroupId));
            CreateMap<UpdateStudentScheduleDto, StudentSchedule>();

            CreateMap<StudentSchedule, StudentScheduleListDto>()
                .ForMember(dest => dest.Teacher, opt => opt.MapFrom(src => src.TeacherSchedule.Teacher))
                .ForMember(dest => dest.TeacherScheduleStatus, opt => opt.MapFrom(src => src.TeacherScheduleDetails.Status))
                 .ForMember(dest => dest.TeacherScheduleReason, opt => opt.MapFrom(src => src.TeacherScheduleDetails.Reason))
                .ForMember(dest => dest.Course, opt => opt.MapFrom(src => src.TeacherSchedule.Course))
                .ForMember(dest => dest.Classroom, opt => opt.MapFrom(src => src.TeacherSchedule.Classroom))
                .ForMember(dest => dest.StartTime, opt => opt.MapFrom(src => src.TeacherScheduleDetails.StartTime))
                .ForMember(dest => dest.EndTime, opt => opt.MapFrom(src => src.TeacherScheduleDetails.EndTime))
                .ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.Status))
                .ForMember(dest => dest.Reason, opt => opt.MapFrom(src => src.Reason))
                .ForMember(dest => dest.Remarks, opt => opt.MapFrom(src => src.Remarks));
        }
    }



    public class StudentScheduleGradeResolver : IValueResolver<StudentSchedule, StudentScheduleDto, GradeEnum?>
    {
        // 静态缓存配置值
        private static int? _cachedMonth = null;
        private static int? _cachedDay = null;
        private static DateTime _lastCacheTime = DateTime.MinValue;
        private static readonly TimeSpan _cacheExpiration = TimeSpan.FromSeconds(10);

        public GradeEnum? Resolve(
            StudentSchedule source,
            StudentScheduleDto destination,
            GradeEnum? destMember,
            ResolutionContext context
        )
        {
            // 获取缓存的配置值
            GetCachedConfigValues(out int month, out int day);
            return source.Student.Grade.GetGradeByEnrollmentDate(source.Student.EnrollmentDate, month, day);
        }

        // 获取缓存的配置值
        private static void GetCachedConfigValues(out int month, out int day)
        {
            // 检查缓存是否过期
            if (
                _cachedMonth == null
                || _cachedDay == null
                || DateTime.Now - _lastCacheTime > _cacheExpiration
            )
            {
                // 从配置中获取值
                _cachedMonth = ServiceLocator.GetConfigIntValue("grade11Month", 10);
                _cachedDay = ServiceLocator.GetConfigIntValue("grade11Data", 1);
                _lastCacheTime = DateTime.Now;
            }
            month = _cachedMonth.Value;
            day = _cachedDay.Value;
        }
    }

  
}
